import NProgress from 'nprogress' // 网页顶部进度
import 'nprogress/nprogress.css' // 进度条样式
NProgress.configure({ showSpinner: false }) // 隐藏旋转图标

import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'

Vue.use(Router)

const { routers } = store.state.permission

const router = new Router({
  // mode: 'history', // require service support (另外支持hash)
  scrollBehavior: () => ({ y: 0 }),
  routes: routers
})

router.beforeEach((to, from, next) => {
  NProgress.start() // 进度条开始
  const { token } = store.state.user
  if (!token && to.path !== '/login') {
    next({ path: '/login' })
  } else if (token && to.path == '/login') {
    next({ path: '/' })
  } else {
    next()
  }
  NProgress.done() // 进度条结束
})

router.afterEach(() => {
  NProgress.done() // 进度条结束
})

export default router
